using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;

namespace LowLevelGraphics.ImageAcquiring
{

    /// <summary>
    /// 
    /// </summary>
	public abstract class OnlineSearchProvider : IOnlineSearchProvider
	{
		public event EventHandler FileDownloaded;

		protected string m_sSearch = string.Empty;
//		private bool m_bUseWebClient = true;

		private string m_sHost = string.Empty;

		private string m_sReferrer = string.Empty;

		public virtual void DownloadFiles(object _sSearch)
		{
			DownloadFiles(_sSearch.ToString(), true);
		}

        /// <summary>
        /// Downloads the files.
        /// </summary>
        /// <param name="_sSearch">The _s search.</param>
        /// <param name="_bUseWebClient">if set to <c>true</c> [_b use web client].</param>
		public virtual void DownloadFiles(string _sSearch, bool _bUseWebClient)
		{	
		}

        /// <summary>
        /// Pages the increment.
        /// </summary>
		public void PageIncrement()
		{
		}

        /// <summary>
        /// Gets or sets the host.
        /// </summary>
        /// <value>The host.</value>
		public string Host
		{
			get { return m_sHost; }
			set { m_sHost = value; }
		}

		public string Referrer
		{
			get { return m_sReferrer; }
			set { m_sReferrer = value; }
		}

        /// <summary>
        /// Gets the socket data.
        /// </summary>
        /// <param name="sUrl">The s URL.</param>
        /// <param name="sPath">The s path.</param>
        /// <param name="sHost">The s host.</param>
        /// <returns></returns>
		protected static string GetSocketData(string sUrl, string sPath, string sHost)
		{
			return GetSocketData(sUrl, sPath, sHost, string.Empty);
		}

        /// <summary>
        /// Gets the socket data.
        /// </summary>
        /// <param name="sUrl">The s URL.</param>
        /// <param name="sPath">The s path.</param>
        /// <param name="sHost">The s host.</param>
        /// <param name="sReferrer">The s referrer.</param>
        /// <returns></returns>
		protected static string GetSocketData(string sUrl, string sPath, string sHost, string sReferrer)
		{
			TcpClient client = new TcpClient(sUrl, 80);
			client.SendBufferSize = 16384;
			client.NoDelay = false;
			NetworkStream stream = client.GetStream();
			string sText = string.Empty;
			sText += "GET /" + sPath + " HTTP/1.1\r\n";
			sText += "Host: " + sHost + "\r\n";
			sText += "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6 (.NET CLR 3.5.30729)\r\n";
			sText += "Accept-Encoding: deflate\r\n";
			sText += "Referer: "+sReferrer+"r\n";
			sText += "\r\n\r\n";
			byte[] aSend = Encoding.ASCII.GetBytes(sText);


			client.ReceiveBufferSize = 32768;
			stream.Write(aSend, 0, aSend.Length);
			byte[] aByte = new byte[client.ReceiveBufferSize];
			int nCount = stream.Read(aByte, 0, (int)client.ReceiveBufferSize);
			String sData = Encoding.ASCII.GetString(aByte);
			//char[] achUnused = { (char)sData[nCount] };
			//Console.WriteLine(sData.TrimEnd(achUnused));
			stream.Close();
			client.Close();
			return sData;
		}
	}
}
